<?php

namespace app\airobot\controller;

use app\airobot\CommonLogic;
use app\airobot\model\OriginRobotBox;
use app\airobot\model\RobotProduct;
use app\airobot\model\UserModel;
use think\admin\Controller;
use think\facade\Db;

/**
 * 超管系统-盒子管理
 * Class Box
 * @package app\airobot\controller
 */
class TestBox extends Controller
{
    private $table = 'robot_box';
    public $commonLogic;

    /**
     * 盒子列表
     * @auth true
     */
    public function index()
    {
        $this->commonLogic = new CommonLogic();
        $this->title = '盒子列表';
        $query = $this->_query($this->table);
        $where = [];
        if (!empty($_GET['user_id'])) {
            $where['from_user_id'] = $_GET['user_id'];
        }
        $query->where($where);
        $query->order('id asc')->page();
    }

    protected function _index_page_filter(array &$data)
    {
        $this->commonLogic = new CommonLogic();
        $ImageUrl = CommonLogic::GetImageUrl();
        // 用户
        $userIds = array_column($data, 'from_user_id');
        $userModel = new UserModel();
        $userList = $userModel->queryList([], ['field' => 'id', 'data' => $userIds], [], [], [], "", $field = "*", $order = "");
        $userMap = array_column($userList, null, 'id');

        // 商品
        $productIds = [];
        foreach ($data as $vo) {
            $showProductsList = explode(";", $vo['show_products']);
            foreach ($showProductsList as $showProductsItem) {
                $showProductsItemArr = explode("|", $showProductsItem);
                $productIds[] = $showProductsItemArr[0];
            }
        }
        $productIds = array_unique($productIds);
        $productModel = new RobotProduct();
        $productList = $productModel->queryList([], ['field' => 'id', 'data' => $productIds], [], [], [], "", $field = "*", $order = "");
        $productMap = array_column($productList, null, 'id');
        foreach ($data as &$vo) {
            $vo['username'] = isset($userMap[$vo['from_user_id']]) ? $userMap[$vo['from_user_id']]['username'] : '';
            $vo['amount'] = $this->commonLogic->DealAmount($vo['amount']);
            if ($vo['description'] == "") {
                $vo['description'] = $vo['name'];
            }
            if (strlen($vo['description']) > 50) {
                $vo['description'] = $this->commonLogic->TruncateString($vo['description'], 50) . "...";
            }
            if ($vo['from_user_id'] == "0") {
                $vo['from_user_id'] = "系统";
            }
            $vo['image'] = $ImageUrl['box'] . $vo['image'];
            // 展示商品
            $productIdsArr = [];
            $showProductsList = explode(";", $vo['show_products']);
            foreach ($showProductsList as $showProductsItem) {
                $showProductsItemArr = explode("|", $showProductsItem);
                $productIdsArr[] = $showProductsItemArr[0];
            }
            $productTotalAmount = 0;
            $showProductsImage = [];
            foreach ($productIdsArr as $productId) {
                // 金额
                $productAmount = isset($productMap[$productId]) ? $productMap[$productId]['amount'] : '0';
                $productTotalAmount = (float)$productTotalAmount + (float)$productAmount;
                // 图片
                $productImage = isset($productMap[$productId]) ? $ImageUrl['product'] . $productMap[$productId]['image'] : '';
                if ($productImage != "") {
                    $showProductsImage[] = $productImage;
                }
            }
            $vo['product_total_amount'] = $this->commonLogic->DealAmount($productTotalAmount);
            $vo['show_products_image'] = $showProductsImage;
        }
    }

    /**
     * 查看rtp
     * @auth true
     */
    public function queryrtp()
    {
        $boxModel = new \app\airobot\model\RobotBox();
        $vo = $boxModel->queryInfo(["id" => $_GET["id"]]);
        $rtpArr = json_decode($vo["rtp"], true);
        $index = 0;
        foreach ($rtpArr as $k => $rtpItem) {
            $vo["RTP" . (++$index)] = $k . "==" . $rtpItem;
        }
        $this->vo = $vo;
        OriginRobotBox::mForm('rtp');
    }
}